package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.lowcode.DeveloperService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.developer.pojo.dto.DeveloperAddDTO;
import com.xbongbong.pro.developer.pojo.dto.DeveloperDeleteDTO;
import com.xbongbong.pro.developer.pojo.dto.DeveloperGetDTO;
import com.xbongbong.pro.developer.pojo.dto.DeveloperListDTO;
import com.xbongbong.pro.developer.pojo.dto.DeveloperUpdateDTO;
import com.xbongbong.pro.developer.pojo.vo.DeveloperAddVO;
import com.xbongbong.pro.developer.pojo.vo.DeveloperDeleteVO;
import com.xbongbong.pro.developer.pojo.vo.DeveloperGetVO;
import com.xbongbong.pro.developer.pojo.vo.DeveloperListVO;
import com.xbongbong.pro.developer.pojo.vo.DeveloperUpdateVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/03/03 17:18
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/developer")
public class DeveloperController {


    @Resource
    private DeveloperService developerService;

    /**
     * 开发人员列表
     *
     * @param developerListDTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid DeveloperListDTO developerListDTO, BindingResult br) throws Exception {
        XbbResponse<DeveloperListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeveloperListVO developerListVO = developerService.list(developerListDTO);
                response = new XbbResponse<>(developerListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开发人员新建
     *
     * @param developerAddDTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid DeveloperAddDTO developerAddDTO, BindingResult br) throws Exception {
        XbbResponse<DeveloperAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeveloperAddVO developerAddVO = developerService.add(developerAddDTO);
                response = new XbbResponse<>(developerAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开发人员更新
     *
     * @param developerUpdateDTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid DeveloperUpdateDTO developerUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<DeveloperUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeveloperUpdateVO developerAddVO = developerService.update(developerUpdateDTO);
                response = new XbbResponse<>(developerAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除
     *
     * @param developerDeleteDTO
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid DeveloperDeleteDTO developerDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<DeveloperDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeveloperDeleteVO developerAddVO = developerService.delete(developerDeleteDTO);
                response = new XbbResponse<>(developerAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * get
     *
     * @param developerGetDTO
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid DeveloperGetDTO developerGetDTO, BindingResult br) throws Exception {
        XbbResponse<DeveloperGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DeveloperGetVO developerGetVO = developerService.get(developerGetDTO);
                response = new XbbResponse<>(developerGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
